const Queue = require('./queue')

/*
let q = new Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)

q.dequeue()

q.log()
*/
// 1. 竞技游戏匹配对手和玩家的功能
/*
class GamePlayer {
    constructor(id, score) {
        this.id = id
        this.score = score
    }
}

let queue = new Queue()
queue.enqueue(new GamePlayer(1, 3000))
queue.enqueue(new GamePlayer(2, 3000))
queue.enqueue(new GamePlayer(3, 9000))
queue.enqueue(new GamePlayer(4, 3000))
queue.enqueue(new GamePlayer(5, 3000))

queue.enqueue(new GamePlayer(6, 3000))
queue.enqueue(new GamePlayer(7, 6000))
queue.enqueue(new GamePlayer(8, 2000))
queue.enqueue(new GamePlayer(9, 2000))
queue.enqueue(new GamePlayer(10, 8000))
queue.enqueue(new GamePlayer(11, 2000))
queue.enqueue(new GamePlayer(12, 2000))
queue.enqueue(new GamePlayer(13, 2000))

const createRoom = (queue) => {
    let rooms = []
    let ordinaryQueue = new Queue()
    let seniorQueue = new Queue()

    while(queue.size() > 0) {
        let gamePlayer = queue.dequeue()
        if (gamePlayer.score > 5000) {
            seniorQueue.enqueue(gamePlayer)
        } else {
            ordinaryQueue.enqueue(gamePlayer)
        }
    }

    while(ordinaryQueue.size() >= 5) {
        let room = []
        
        for (let i = 0; i < 5; i++) {
            room.push(ordinaryQueue.dequeue())
        }

        rooms.push(room)
    }
    console.log(rooms)
    seniorQueue.log()
    return rooms
}

createRoom(queue)
 */

// 2. 用队列给数字排序

/*
    95 55 64 34 12 19 66

    // 用个位数, 分组
    [
        0-queue
        1-queue
        2-queue 12
        3-queue
        4-queue 64, 34
        5-queue 95, 55
        6-queue 66
        7-queue
        8-queue
        9-queue 19
    ]
    [
        0-queue
        1-queue 12 19
        2-queue 
        3-queue 34
        4-queue
        5-queue 55
        6-queue 66 64
        7-queue
        8-queue
        9-queue 95
    ]
*/
/**
let nums = [95, 55, 64, 34, 12, 19, 66]
let queueList = []
for (let i = 0; i < 10; i++) {
    queueList.push(new Queue())
}

// 分配数字
function distribute(nums, queueList, digit) {
    for (let i = 0; i < nums.length; i++) {
        let num = nums[i]
        if (digit === 1) {
            queueList[num % 10].enqueue(num)
        } else {
            queueList[Math.floor(num / 10)].enqueue(num)
        }
    }
    console.log(
        queueList
    )
}

const collect = (queueList) => {
    let nums = []
    let i = 0
    for (let j = 0; j < queueList.length; j++) {
        while(!queueList[j].isEmpty()) {
            nums[i++] = queueList[j].dequeue()
        }
    }
    return nums
}

distribute(nums, queueList, 1)
console.log(
    collect(queueList)
)

distribute(nums, queueList, 10)
console.log(
    collect(queueList)
)
 */

// 3. 约瑟夫环

/*
    [0,1,,,4,,6,,8,9 ... 99]
    [?]
*/

let arr = []
for (let i = 0; i < 100; i++) {
    arr.push(i)
}

const ring = (arr) => {
    let queue = new Queue()
    for (let item of arr) {
        queue.enqueue(item)
    }
    
    let index = 0
    while(queue.size() !== 1) {
        let item = queue.dequeue()
        index++
        if (index % 3 !== 0) {
            queue.enqueue(item)
        }
    }

    return queue.top()
}

console.log(ring(arr))
